package com.sky.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.BaseException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealOverViewVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Resource
    SetmealMapper setmealMapper;

    @Resource
    SetmealDishMapper setmealDishMapper;
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        int page = setmealPageQueryDTO.getPage();
        int pageSize = setmealPageQueryDTO.getPageSize();
        String name = setmealPageQueryDTO.getName();
        Integer categoryId = setmealPageQueryDTO.getCategoryId();
        Integer status = setmealPageQueryDTO.getStatus();
        PageHelper.startPage(page, pageSize);
        Page<Setmeal> setmealList = setmealMapper.pageQuery(name, categoryId, status);

        // 填充信息
        PageResult pageResult = new PageResult();
        pageResult.setRecords(setmealList.getResult());
        pageResult.setTotal(setmealList.getTotal());

        return pageResult;
    }

    @Override
    public SetmealVO getById(Long id) {

        Setmeal setmeal = setmealMapper.getById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);

        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);

        setmealVO.setSetmealDishes(setmealDishes);

        return setmealVO;
    }

    @Transactional
    @Override
    public void save(SetmealDTO setmealDTO) {
        Long currentId = BaseContext.getCurrentId();
        if(currentId != 1L) {
            throw new RuntimeException(MessageConstant.NO_AUTH);
        }

        Long categoryId = setmealDTO.getCategoryId();
        String name = setmealDTO.getName();
        BigDecimal price = setmealDTO.getPrice();
        Integer status = setmealDTO.getStatus();
        String image = setmealDTO.getImage();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(categoryId == null || !StrUtil.isNotBlank(image) || !StrUtil.isNotBlank(name)
        || price == null || setmealDishes == null || setmealDishes.size() == 0 || status == null) {
            throw new RuntimeException(MessageConstant.PARAM_ERROR);
        }

        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.save(setmeal);

        Long setmealId = setmeal.getId();

        List<SetmealDish> collect = setmealDishes.stream().map((setmealDish) -> {
            setmealDish.setSetmealId(setmealId);
            return setmealDish;
        }).collect(Collectors.toList());

        setmealDishMapper.saveBatch(collect);
    }

    @Override
    public void changeStatus(Integer status, Long id) {
        Long currentId = BaseContext.getCurrentId();
        if(currentId != 1) {
            throw new RuntimeException(MessageConstant.NO_AUTH);
        }
        if(status == null || id == null) {
            throw new RuntimeException(MessageConstant.PARAM_ERROR);
        }
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);

        setmealMapper.changeStatus(setmeal);
    }

    @Transactional
    @Override
    public void update(SetmealDTO setmealDTO) {
        Long id = setmealDTO.getId();
        Long categoryId = setmealDTO.getCategoryId();
        String name = setmealDTO.getName();
        BigDecimal price = setmealDTO.getPrice();
        Integer status = setmealDTO.getStatus();
        String image = setmealDTO.getImage();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();


        if(categoryId == null || !StrUtil.isNotBlank(image) || !StrUtil.isNotBlank(name)
                || price == null || setmealDishes == null || setmealDishes.size() == 0 || status == null) {
            throw new RuntimeException(MessageConstant.PARAM_ERROR);
        }
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        // 直接更新setmeal内容
        setmealMapper.update(setmeal);


        if(setmealDishes != null && setmealDishes.size() > 0) {
            // 先删除原本的内容
            setmealDishMapper.delectBySetmealId(id);
            List<SetmealDish> collect = setmealDishes.stream().map(setmealDish -> {
                setmealDish.setSetmealId(id);
                return setmealDish;
            }).collect(Collectors.toList());
            // 在将更新的内容插入进去
            setmealDishMapper.saveBatch(collect);
        }
    }

    @Override
    public void deleteBatchByIds(List<Long> ids) {
        if(ids == null || ids.size() == 0) {
            return;
        }
        Long currentId = BaseContext.getCurrentId();
        if(currentId != 1) {
            throw new BaseException(MessageConstant.NO_AUTH);
        }
        for (Long id : ids) {
            // 先删除setmealid为当前id的setmealDish
            setmealDishMapper.delectBySetmealId(id);
            // 删除id为当前id的setmeal
            setmealMapper.deleteById(id);
        }
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemById(id);
    }

    @Override
    public SetmealOverViewVO overviewSetmeals() {
        Integer sold = setmealMapper.getByStatus(StatusConstant.ENABLE);
        Integer discontinued = setmealMapper.getByStatus(StatusConstant.DISABLE);
        return SetmealOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }


}
